package com.amos.muexamclient.sys.service.impl;

import com.amos.exception.BizExceptionEnum;
import com.amos.muexamclient.sys.entity.SysPostEntity;
import com.amos.muexamclient.sys.mapper.SysPostMapper;
import com.amos.muexamclient.sys.service.SysPostService;
import com.amos.muexamclient.sys.service.SysUserPostService;
import com.amos.muexamclient.sys.service.dto.SysPostDTO;
import com.amos.muexamcore.core.AbstractPageService;
import com.amos.muexamcore.core.BaseMapper;
import com.amos.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Copyright © 2018 五月工作室. All rights reserved.
 *
 * @Package com.amos.muexamclient.sys.service.impl
 * @ClassName SysPostServiceImpl
 * @Description TODO
 * @Author Amos
 * @Modifier
 * @Date 2020-03-24 14:35:49
 * @Version 1.0
 **/
@Service
public class SysPostServiceImpl extends AbstractPageService<SysPostDTO, SysPostEntity> implements SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private SysUserPostService userPostService;

    /**
     * 条件查询
     *
     * @param dto 查询的条件
     */
    @Override
    public List<SysPostDTO> list(SysPostDTO dto) {
        return this.getMapper().select(dto);
    }

    /**
     * 返回BaseDao对象
     */
    @Override
    public BaseMapper<SysPostEntity, SysPostDTO> getMapper() {
        return sysPostMapper;
    }

    /**
     * 保存之间的校验
     *
     * @param dto 保存的对象
     */
    @Override
    public void preValidate(SysPostDTO dto) {
        this.validatePostCode(dto.getPostCode(), dto.getId());

        this.validatePostName(dto.getPostName(), dto.getId());
    }

    private void validatePostCode(String postCode, String id) {
        SysPostDTO dto = new SysPostDTO();
        dto.setPostCode(postCode);
        List<SysPostDTO> list = this.list(dto);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        dto = list.get(0);
        if (StringUtils.isNullOrEmpty(id)) {
            BizExceptionEnum.DEFINED_EXCEPTION.definedMsg("新增岗位'" + dto.getPostName() + "'失败，岗位编码已存在").throwException();
        }
        if (dto.getId().equals(id)) {
            BizExceptionEnum.DEFINED_EXCEPTION.definedMsg("修改岗位'" + dto.getPostName() + "'失败，岗位编码已存在").throwException();
        }
    }

    private void validatePostName(String postName, String id) {
        SysPostDTO dto = new SysPostDTO();
        dto.setPostName(postName);
        List<SysPostDTO> list = this.list(dto);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        dto = list.get(0);
        if (StringUtils.isNullOrEmpty(id)) {
            BizExceptionEnum.DEFINED_EXCEPTION.definedMsg("新增岗位'" + dto.getPostName() + "'失败，岗位名称已存在").throwException();
        }
        if (!dto.getId().equals(id)) {
            BizExceptionEnum.DEFINED_EXCEPTION.definedMsg("修改岗位'" + dto.getPostName() + "'失败，岗位名称已存在").throwException();
        }
    }

    /**
     * 添加子集数据
     *
     * @param id  主表ID
     * @param dto 主表DTO对象
     */
    @Override
    public void appendSubsidiary(String id, SysPostDTO dto) {

    }

    @Override
    public int remove(String[] postIds) {
        for (String postId : postIds) {
            SysPostDTO post = this.load(postId);
            int index = userPostService.countUserPostById(postId);
            if (index > 0) {
                BizExceptionEnum.DEFINED_EXCEPTION.definedMsg(String.format("%1$s已分配,不能删除", post.getPostName())).throwException();
            }
        }
        return this.sysPostMapper.deletePostByIds(postIds);
    }

    @Override
    public List<SysPostDTO> selectPostByUserId(String id) {
        return this.sysPostMapper.selectPostByUserId(id);
    }
}
